package com.wwz.demo;

import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.wwz.utils.MyBeanUtil;
import okhttp3.*;
import org.junit.Test;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Export {

    @Test
    public void write() throws IOException, InvocationTargetException, IllegalAccessException {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(500, TimeUnit.SECONDS)
                .readTimeout(500, TimeUnit.SECONDS)
                .writeTimeout(500, TimeUnit.SECONDS)
                .retryOnConnectionFailure(false)
                .build();

        ContractListParamRequest contractListParamRequest = new ContractListParamRequest();
        contractListParamRequest.setStatusList(Collections.singletonList(4));
        RequestBody requestBody = RequestBody.create(MediaType.parse(org.springframework.http.MediaType.APPLICATION_JSON_VALUE), JacksonUtil.obj2String(contractListParamRequest));


        Request request = new Request.Builder()
                .url("http://sep-xueshan.snowballfinance.com//brand/contract/list.json")
                .post(requestBody)
                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36")
                .addHeader("Cookie", "experimentation_subject_id=eyJfcmFpbHMiOnsibWVzc2FnZSI6IklqUmtPRGc0WkdKa0xUYzROamd0TkdVeVpDMDRaR0V6TFROaU9EQXhOelUxTnpNM1lpST0iLCJleHAiOm51bGwsInB1ciI6ImNvb2tpZS5leHBlcmltZW50YXRpb25fc3ViamVjdF9pZCJ9fQ%3D%3D--bf2cb51ae809a7a9ff34f1b174cec818877b8326; _ga=GA1.2.30974909.1614675518; _hjid=f1a1289b-0aa3-4313-b369-46bca0b81ea1; _gid=GA1.2.1984908179.1625448890; JSESSIONID=ea284894-c6ae-497d-b343-f5ab58e39d3b; username=wangwanzhao")
                .addHeader("Content-Type", "application/json;charset=UTF-8")
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            throw new IOException("Unexpected code " + response);
        }

        assert response.body() != null;
        String respStr = response.body().string();
        JsonObject departmentJsonObj = com.google.gson.JsonParser.parseString(respStr).getAsJsonObject();
        JsonArray asJsonArray = departmentJsonObj.getAsJsonObject("data").getAsJsonArray("list");

        ArrayList<ContractExtVO> contractExtVOS = JacksonUtil.string2Obj(asJsonArray.toString(), new TypeReference<ArrayList<ContractExtVO>>() {
        });
        System.out.println(contractExtVOS.size());
        String fileName = "/Users/sailor/Desktop/source/合同导出"+ System.currentTimeMillis() + ".xlsx";

        List<Entity> list = MyBeanUtil.convertList2List(contractExtVOS,Entity.class);
        EasyExcel.write(fileName, Entity.class).sheet("模板").doWrite(list);
    }



}
